+page.svelte 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <script>
  2. import {page} from "$app/state";
  3. import {PUBLIC_API_URL} from "$env/static/public";
  4. import {onMount} from "svelte";
  5. import DashboardNav from "../../dashboard/DashboardNav.svelte";
  6. import Message from "./Message.svelte";
  7. let game = $state(page.data.data.game);
  8. let character = $state(page.data.data.character);
  9. let sessions = $state(page.data.data.sessions.sort((a, b) => a.started_at > b.started_at ? 1 : -1));
  10. let turns = $state(page.data.data.turns);
  11. let tokensRemaining = $state(page.data.data.tokens_remaining);
  12. let streaming = $state(false);
  13. let inputText = $state("");
  14. const autogrow = (node) => {
  15. const update = () => {
  16. node.style.height = "auto";
  17. node.style.height = Math.min(node.scrollHeight, 200) + "px";
  18. };
  19. node.addEventListener("input", update);
  20. return { destroy: () => node.removeEventListener("input", update) };
  21. };
  22. const handleSubmit = (e) => {
  23. e.preventDefault();
  24. const text = inputText.trim();
  25. if (!text || streaming) return;
  26. inputText = "";
  27. sendMessage(text);
  28. };
  29. const sendMessage = async (text)=>{
  30. let newTurn = {user_text: text};
  31. turns.push(newTurn);
  32. const response = await fetch(`${PUBLIC_API_URL}/game/${game.id}/session/${sessions[0].id}/turn`, {
  33. method: "POST",
  34. headers: {
  35. "Content-Type": "application/json",
  36. "Accept-Encoding": "identity"
  37. },
  38. credentials: "include",
  39. body: JSON.stringify({content: text})
  40. });
  41. const reader = response.body.getReader();
  42. const decoder = new TextDecoder();
  43. let buffer = "";
  44. while(true){
  45. const {done, value} = await reader.read();
  46. if(done) break;
  47. buffer += decoder.decode(value, {stream: true});
  48. const lines = buffer.split("\n");
  49. buffer = lines.pop();
  50. for(let i = 0; i < lines.length; i++){
  51. if(!lines[i].startsWith("data:")) continue;
  52. const data = lines[i].slice(5).trim();
  53. if(data === "[DONE]") continue;
  54. try{
  55. const json = JSON.parse(data);
  56. const chunk = json.choices?.[0]?.delta?.content;
  57. if(chunk) turns[turns.length-1].llm_text += chunk;
  58. }catch{}
  59. }
  60. }
  61. streaming = false;
  62. }
  63. onMount(()=>{
  64. if(turns.length === 0){
  65. sendMessage("");
  66. }
  67. });
  68. </script>
  69. <div class="page">
  70. <DashboardNav />
  71. <div class="game-bar">
  72. <div class="game-bar-left">
  73. <div class="game-title-block">
  74. <span class="game-label">Adventure</span>
  75. <h1>{game.title}</h1>
  76. </div>
  77. <div class="divider"></div>
  78. <div class="character-block">
  79. <span class="game-label">Character</span>
  80. <span class="character-name">{character.name}</span>
  81. </div>
  82. </div>
  83. <div class="token-badge" class:token-low={tokensRemaining < 500}>
  84. <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
  85. <circle cx="12" cy="12" r="10"/>
  86. <path d="M12 6v12M9 9h4.5a2.5 2.5 0 0 1 0 5H9"/>
  87. </svg>
  88. {tokensRemaining.toLocaleString()} tokens
  89. </div>
  90. </div>
  91. <main class="messages" id="message-list">
  92. <div class="messages-inner">
  93. {#each turns as turn, i}
  94. {#if i !== 0}
  95. <Message {turn} role="user" />
  96. {/if}
  97. <Message
  98. {turn}
  99. role="llm"
  100. streaming={streaming && i === turns.length - 1}
  101. />
  102. {/each}
  103. {#if streaming}
  104. <div class="typing-indicator">
  105. <span></span><span></span><span></span>
  106. </div>
  107. {/if}
  108. </div>
  109. </main>
  110. <div class="composer-wrap">
  111. <form class="composer" onsubmit={handleSubmit}>
  112. <textarea
  113. bind:value={inputText}
  114. use:autogrow
  115. placeholder="What do you do?"
  116. rows="1"
  117. disabled={streaming}
  118. onkeydown={(e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleSubmit(e); } }}
  119. ></textarea>
  120. <button type="submit" disabled={streaming || !inputText.trim()} aria-label="Send message">
  121. <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.25" stroke-linecap="round" stroke-linejoin="round">
  122. <path d="M12 19V5M5 12l7-7 7 7"/>
  123. </svg>
  124. </button>
  125. </form>
  126. <p class="composer-hint">Enter to send &nbsp;·&nbsp; Shift+Enter for new line</p>
  127. </div>
  128. </div>
  129. <style>
  130. .page {
  131. height: 100vh;
  132. display: flex;
  133. flex-direction: column;
  134. background: #0b0b10;
  135. overflow: hidden;
  136. }
  137. /* ── Game bar ── */
  138. .game-bar {
  139. display: flex;
  140. align-items: center;
  141. justify-content: space-between;
  142. padding: 0.75rem 2rem;
  143. border-bottom: 1px solid #1a1a26;
  144. background: #0d0d16;
  145. flex-shrink: 0;
  146. gap: 1rem;
  147. }
  148. .game-bar-left {
  149. display: flex;
  150. align-items: center;
  151. gap: 1.25rem;
  152. min-width: 0;
  153. }
  154. .game-label {
  155. display: block;
  156. font-size: 0.65rem;
  157. font-weight: 600;
  158. letter-spacing: 0.08em;
  159. text-transform: uppercase;
  160. color: #44445a;
  161. margin-bottom: 0.15rem;
  162. }
  163. h1 {
  164. font-size: 0.95rem;
  165. font-weight: 700;
  166. color: #e5e5ea;
  167. letter-spacing: -0.2px;
  168. white-space: nowrap;
  169. overflow: hidden;
  170. text-overflow: ellipsis;
  171. }
  172. .divider {
  173. width: 1px;
  174. height: 2rem;
  175. background: #1e1e2e;
  176. flex-shrink: 0;
  177. }
  178. .character-name {
  179. font-size: 0.95rem;
  180. font-weight: 600;
  181. color: #c084fc;
  182. }
  183. .token-badge {
  184. display: inline-flex;
  185. align-items: center;
  186. gap: 5px;
  187. padding: 0.35rem 0.75rem;
  188. border-radius: 20px;
  189. background: #13131e;
  190. border: 1px solid #1e1e2e;
  191. font-size: 0.78rem;
  192. font-weight: 600;
  193. color: #6b6b7a;
  194. white-space: nowrap;
  195. flex-shrink: 0;
  196. transition: color 0.2s, border-color 0.2s;
  197. }
  198. .token-badge.token-low {
  199. color: #e05555;
  200. border-color: rgba(224, 85, 85, 0.3);
  201. }
  202. /* ── Messages ── */
  203. .messages {
  204. flex: 1;
  205. overflow-y: auto;
  206. scroll-behavior: smooth;
  207. }
  208. .messages::-webkit-scrollbar {
  209. width: 6px;
  210. }
  211. .messages::-webkit-scrollbar-track {
  212. background: transparent;
  213. }
  214. .messages::-webkit-scrollbar-thumb {
  215. background: #1e1e2e;
  216. border-radius: 3px;
  217. }
  218. .messages-inner {
  219. max-width: 820px;
  220. margin: 0 auto;
  221. padding: 2rem 2rem 1rem;
  222. display: flex;
  223. flex-direction: column;
  224. gap: 1rem;
  225. }
  226. /* ── Typing indicator ── */
  227. .typing-indicator {
  228. display: flex;
  229. align-items: center;
  230. gap: 5px;
  231. padding: 0.75rem 1rem;
  232. background: #1a1a28;
  233. border: 1px solid #2a2a3a;
  234. border-radius: 16px;
  235. border-bottom-left-radius: 4px;
  236. width: fit-content;
  237. }
  238. .typing-indicator span {
  239. width: 7px;
  240. height: 7px;
  241. border-radius: 50%;
  242. background: #44445a;
  243. animation: bounce 1.2s ease-in-out infinite;
  244. }
  245. .typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
  246. .typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
  247. @keyframes bounce {
  248. 0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  249. 30% { transform: translateY(-5px); opacity: 1; }
  250. }
  251. /* ── Composer ── */
  252. .composer-wrap {
  253. flex-shrink: 0;
  254. border-top: 1px solid #1a1a26;
  255. background: #0d0d16;
  256. padding: 1rem 2rem 0.6rem;
  257. }
  258. .composer {
  259. max-width: 820px;
  260. margin: 0 auto;
  261. display: flex;
  262. align-items: flex-end;
  263. gap: 0.75rem;
  264. background: #13131e;
  265. border: 1px solid #2a2a3a;
  266. border-radius: 14px;
  267. padding: 0.6rem 0.6rem 0.6rem 1rem;
  268. transition: border-color 0.2s, box-shadow 0.2s;
  269. }
  270. .composer:focus-within {
  271. border-color: #5a5aff;
  272. box-shadow: 0 0 0 3px rgba(90, 90, 255, 0.1);
  273. }
  274. .composer textarea {
  275. flex: 1;
  276. background: none;
  277. border: none;
  278. outline: none;
  279. resize: none;
  280. font-size: 0.95rem;
  281. line-height: 1.6;
  282. color: #e5e5ea;
  283. font-family: inherit;
  284. min-height: 26px;
  285. max-height: 200px;
  286. overflow-y: auto;
  287. padding: 0;
  288. }
  289. .composer textarea::placeholder {
  290. color: #44445a;
  291. font-style: italic;
  292. }
  293. .composer textarea:disabled {
  294. opacity: 0.5;
  295. cursor: not-allowed;
  296. }
  297. .composer button {
  298. width: 36px;
  299. height: 36px;
  300. border-radius: 9px;
  301. border: none;
  302. background: #5a5aff;
  303. color: #fff;
  304. display: flex;
  305. align-items: center;
  306. justify-content: center;
  307. cursor: pointer;
  308. flex-shrink: 0;
  309. transition: background 0.15s, transform 0.1s, opacity 0.15s;
  310. }
  311. .composer button:hover:not(:disabled) {
  312. background: #4646e0;
  313. }
  314. .composer button:active:not(:disabled) {
  315. transform: scale(0.93);
  316. }
  317. .composer button:disabled {
  318. opacity: 0.35;
  319. cursor: not-allowed;
  320. }
  321. .composer-hint {
  322. max-width: 820px;
  323. margin: 0.4rem auto 0;
  324. font-size: 0.7rem;
  325. color: #2e2e44;
  326. text-align: right;
  327. padding: 0 0.25rem;
  328. }
  329. @media (max-width: 700px) {
  330. .game-bar { padding: 0.6rem 1rem; }
  331. .messages-inner { padding: 1.25rem 1rem 0.75rem; }
  332. .composer-wrap { padding: 0.75rem 1rem 0.5rem; }
  333. .game-bar-left { gap: 0.75rem; }
  334. .divider { display: none; }
  335. .character-block { display: none; }
  336. .composer-hint { display: none; }
  337. }
  338. </style>